package cn.btks.resultintegration.rule.service.impl;

import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.rule.dto.rule.YlxzbbQueryParam;
import cn.btks.resultintegration.rule.pojo.Ylxzbb;
import cn.btks.resultintegration.rule.repository.YlxzbbRepository;
import cn.btks.resultintegration.rule.service.YlxzbbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijun
 * @CompileUnit 测绘地理信息分院研发中心
 * @Date 2023/9/22 14:38
 */
@Service
public class YlxzbbServiceImpl implements YlxzbbService {

    @Autowired
    YlxzbbRepository ylxzbbRepository;

    @Override
    public List<Ylxzbb> findAll() {
        return ylxzbbRepository.findAll();
    }

    @Override
    public ResultState findPages(YlxzbbQueryParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNumber() - 1, param.getPageSize());
        Specification<Ylxzbb> specification = new Specification<Ylxzbb>() {
            @Override
            public Predicate toPredicate(Root<Ylxzbb> root,
                                         CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotBlank(param.getHylx())){
                    predicates.add(criteriaBuilder.equal(root.get("hylx"), param.getHylx()));
                }
                if(StringUtils.isNotBlank(param.getYlxzb())){
                    predicates.add(criteriaBuilder.equal(root.get("ylxzb"), param.getYlxzb()));
                }
                if(StringUtils.isNotBlank(param.getJtzb())){
                    predicates.add(criteriaBuilder.equal(root.get("jtzb"), param.getJtzb()));
                }
                Predicate[] predicates1 = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        };
        Page<Ylxzbb> configPage = ylxzbbRepository.findAll(specification, pageRequest);
        if (configPage.getTotalElements() > 0) {
            PageList pageList = new PageList();
            pageList.setResult(configPage.getContent());
            pageList.setPageNum(configPage.getTotalPages());
            pageList.setTotalCount(configPage.getTotalElements());
            return new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(), pageList);
        }
        return new ResultState(StateInfo.FIND_NULL.getCode(), StateInfo.FIND_NULL.getMsg());
    }
}
